package com.zerox.controller;

import com.alibaba.fastjson.JSON;
import com.zerox.entity.SysMenu;
import com.zerox.entity.SysRole;
import com.zerox.service.SysMenuService;
import com.zerox.service.SysRoleService;
import com.zerox.uilt.MenuVO;
import com.zerox.uilt.UserListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
//@RequestMapping("/menu")
public class MenuContorller {

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysRoleService sysRoleService;


    @GetMapping("/index")
    public String list(HttpSession session) {
        return "list";
    }

    @GetMapping("/menuuser")
    public String menuuser(HttpSession session) {
        List<SysRole> roleByName = sysRoleService.roleByname();
        session.setAttribute("roleByName", roleByName);
        return "user/user";
    }

    @GetMapping(value = "/console1")
    public String console1() {
        return "/view/console/console1";
    }

    @GetMapping(value = "/menu1", produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String toMenu(HttpServletRequest request) {
        String username = "";
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("username")) {
                username = cookie.getValue();
            }
        }
//        String username = "admin";
        List<MenuVO> menupates = sysMenuService.menupates(username);
        List<MenuVO> menuchilds = sysMenuService.menuchilds(username);
        ArrayList<Object> parentMenuList = new ArrayList<>();
        // 循环 父级菜单
        for (MenuVO sysMenu : menupates) {
            // 用map封装单个菜单信息
            LinkedHashMap<String, Object> menuInfoMap_parent = new LinkedHashMap<>();
            menuInfoMap_parent.put("id", sysMenu.getId());
            menuInfoMap_parent.put("title", sysMenu.getTitle());
            menuInfoMap_parent.put("icon", sysMenu.getIcon());
            menuInfoMap_parent.put("type", sysMenu.getType());
            menuInfoMap_parent.put("href", (sysMenu.getHref() == null ? "" : sysMenu.getHref()));
            ArrayList<Object> childrenMenuList = new ArrayList<>();
            for (MenuVO child : menuchilds) {
                LinkedHashMap<String, Object> menuInfoMap_child = new LinkedHashMap<>();
                menuInfoMap_child.put("id", child.getId());
                menuInfoMap_child.put("title", child.getTitle());
                menuInfoMap_child.put("icon", child.getIcon());
                menuInfoMap_child.put("type", child.getType());
                menuInfoMap_child.put("href", (child.getHref() == null ? "" : child.getHref()));
                childrenMenuList.add(menuInfoMap_child);
            }
            menuInfoMap_parent.put("children", childrenMenuList);
            parentMenuList.add(menuInfoMap_parent);
        }

        String json = JSON.toJSONString(parentMenuList);

        return json;
    }

    @GetMapping("tomenu")
    public String menu() {
        return "menu/menu";
    }

    @GetMapping("menu")
    @ResponseBody
    public String menuList(@RequestParam(value = "menuName", required = false) String menuName, Integer pageIndex, Integer size) {
        List<SysMenu> menuList = sysMenuService.list(menuName, pageIndex, size);
        System.out.println(menuList);
        int count = sysMenuService.count(menuName);
        UserListVO userListVO = new UserListVO(0, count, menuList, "");
        return JSON.toJSONString(userListVO);
    }


    @GetMapping("tomenuadd")
    public String toAdd() {
        return "menu/add";
    }

    @GetMapping(value = "menuadd", produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String add(SysMenu sysMenu) {
        int row = sysMenuService.add(sysMenu);
        return row > 0 ? "1" : "0";
    }

    @GetMapping("menuDelete/{menuId}")
    @ResponseBody
    public String delete(@PathVariable("menuId") Integer menuId) {
        int row = sysMenuService.delete(menuId);

        return row > 0 ? "1" : "0";
    }

    @GetMapping("selectById/{menuId}")
    public String selectById(@PathVariable("menuId") Integer menuId, HttpServletRequest request) {
        SysMenu sysMenu = sysMenuService.selectById(menuId);
        request.setAttribute("sysMenu", sysMenu);
        return "menu/edit";
    }


    @PostMapping(value = "update",produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String update(SysMenu sysMenu) {
        int row = sysMenuService.update(sysMenu);

        return row > 0 ? "1" : "0";
    }
}





